package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.interfaces.AdapterInterface;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.dto.IntentDataBean;
import cn.wit.summit.chwan.models.dto.JumpBaseBean;
import cn.wit.summit.chwan.models.response.GameDailyNewResponse;
import cn.wit.summit.chwan.models.response.GetTreviListResponse;
import cn.wit.summit.chwan.ui.base.BaseActivity;
import cn.wit.summit.chwan.ui.base.MyApplication;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.UtilsMy;
import cn.wit.summit.chwan.widgets.ItemListView;

/**
 * Created by Alan on 2018/4/4.
 */

public class TreviFountainListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ViewBean> mDatas;
    private Activity mContext;
    private LayoutInflater inflater;
    private boolean isFormCreateTrevi;

    private AdapterInterface mAdapterInterface;

    public TreviFountainListAdapter(Activity context, List<ViewBean> datas, boolean isFormCreateTrevi, AdapterInterface mAdapterInterface) {
        this.mContext = context;
        this.mDatas = datas;
        this.isFormCreateTrevi = isFormCreateTrevi;
        this.mAdapterInterface = mAdapterInterface;
        inflater = LayoutInflater.from(mContext);
    }

    public enum ViewType {
        TYPE_TOP_TITLE,
        TYPE_ITEM_LIST
    }

    public static class ViewBean {
        ViewType viewType;
        Object obj;

        public ViewBean() {
        }

        public ViewBean(ViewType viewType, Object obj) {
            this.viewType = viewType;
            this.obj = obj;
        }

        public ViewType getViewType() {
            return viewType;
        }

        public void setViewType(ViewType viewType) {
            this.viewType = viewType;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }

        public static class TitleBean {
            public String time;

            public TitleBean(String time) {
                this.time = time;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //设置ITEM类型，可以自由发挥，这里设置item position单数显示item1 偶数显示item2
    @Override
    public int getItemViewType(int position) {
            if (mDatas != null) {
                return mDatas.get(position).getViewType().ordinal();
            }
            return -1;
    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder holder = null;

        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == ViewType.TYPE_TOP_TITLE.ordinal()) {
            holder = new TopTitleHolder(inflater.inflate(R.layout.item_trevi_top_view, parent, false));
        } else if (viewType == ViewType.TYPE_ITEM_LIST.ordinal()) {
            holder = new ItemListHolder(inflater.inflate(R.layout.item_trevi_list_view, parent, false));
        }

        return holder;
    }

    //填充onCreateViewHolder方法返回的holder中的控件
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        ViewBean viewBean = mDatas.get(position);
        if (holder instanceof TopTitleHolder) {
            TopTitleHolder mTopTitleHolder = (TopTitleHolder) holder;
            mTopTitleHolder.itemTreviTopRootLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (AccountUtil.getInstance().getUidNotToLogin(mContext) == 0) return;
                    if (isFormCreateTrevi) {
                        mContext.finish();
                        return;
                    }
                    IntentDataBean mIntentDataBean = new IntentDataBean();
                    mIntentDataBean.setJump_link_type(3);
                    mIntentDataBean.setJump_type(17);
                    mIntentDataBean.setJump_link_type_val("17");
                    IntentUtil.getInstance().intentTo(mContext, mIntentDataBean);
                }
            });
        } else if (holder instanceof ItemListHolder) {
            final GetTreviListResponse temp = (GetTreviListResponse) viewBean.getObj();
            ItemListHolder mItemListHolder = (ItemListHolder) holder;

            UtilsMy.setIconImage(mContext, temp.getWish_pond_opt_header_pic(), mItemListHolder.itemTreviIv);
            mItemListHolder.itemTreviGameIv.setImageURI(temp.getWish_pond_game_ico());
            mItemListHolder.itemTreviNameTv.setText(temp.getWish_pond_opt_name());
            mItemListHolder.itemTreviGameNameTv.setText(temp.getWish_pond_title());

            mItemListHolder.itemTreviMakemoneyTv.setText(temp.getWish_pond_amount()+"元");
            mItemListHolder.itemTreviContentTv.setText(temp.getWish_pond_content());
            if (temp.getWish_pond_question_flag().equals("1")) {
                mItemListHolder.itemTreviTimeTv.setText("已解决");
            } else {
                if (temp.getWish_pond_surplus_time()<=0) {
                    mItemListHolder.itemTreviTimeTv.setText("已过期");
                } else {
                    mItemListHolder.itemTreviTimeTv.setText("剩余"+temp.getWish_pond_surplus_time()+"天");
                }
            }
            if (temp.getWish_pond_join_status()==1) {
                mItemListHolder.itemTreviStatusIv.setVisibility(View.VISIBLE);
            } else {
                mItemListHolder.itemTreviStatusIv.setVisibility(View.INVISIBLE);
            }
            mItemListHolder.itemTreviStatusSolutionTv.setText(temp.getWish_pond_solve_amount());
            mItemListHolder.itemTreviStatusSpreadTv.setText(temp.getWish_pond_share_amount());

            mItemListHolder.itemTreviRootLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (AccountUtil.getInstance().getUidNotToLogin(mContext) == 0) return;
                    IntentUtil.getInstance().goShareWebActivity(mContext, temp.getWish_pond_jump_url());
                }
            });
        }
    }

    class TopTitleHolder extends RecyclerView.ViewHolder {
        LinearLayout itemTreviTopRootLl;

        public TopTitleHolder(View view) {
            super(view);
            itemTreviTopRootLl = view.findViewById(R.id.itemTreviTopRootLl);
        }
    }

    class ItemListHolder extends RecyclerView.ViewHolder {
        LinearLayout itemTreviRootLl;
        SimpleDraweeView itemTreviIv;
        TextView itemTreviNameTv;
        TextView itemTreviTimeTv;
        TextView itemTreviMakemoneyTv;
        SimpleDraweeView itemTreviGameIv;
        TextView itemTreviGameNameTv;
        TextView itemTreviContentTv;
        ImageView itemTreviStatusIv;
        TextView itemTreviStatusSolutionTv;
        TextView itemTreviStatusSpreadTv;

        public ItemListHolder(View view) {
            super(view);
            itemTreviIv = view.findViewById(R.id.itemTreviIv);
            itemTreviRootLl = view.findViewById(R.id.itemTreviRootLl);
            itemTreviNameTv = view.findViewById(R.id.itemTreviNameTv);
            itemTreviTimeTv = view.findViewById(R.id.itemTreviTimeTv);
            itemTreviMakemoneyTv = view.findViewById(R.id.itemTreviMakemoneyTv);
            itemTreviGameIv = view.findViewById(R.id.itemTreviGameIv);
            itemTreviGameNameTv = view.findViewById(R.id.itemTreviGameNameTv);
            itemTreviContentTv = view.findViewById(R.id.itemTreviContentTv);
            itemTreviStatusIv = view.findViewById(R.id.itemTreviStatusIv);
            itemTreviStatusSolutionTv = view.findViewById(R.id.itemTreviStatusSolutionTv);
            itemTreviStatusSpreadTv = view.findViewById(R.id.itemTreviStatusSpreadTv);
        }
    }
}
